package com.cn.ycy.proxy.cglib;

import com.cn.ycy.proxy.jdk.Person;
import com.cn.ycy.proxy.jdk.Student;
import net.sf.cglib.proxy.Enhancer;

public class Test {

  @org.junit.Test
  public void test0(){

    Student student = new Student("Jack");

    Enhancer enhancer = new Enhancer();
    enhancer.setCallback(new CustomInterceptor(student));
    enhancer.setSuperclass(Student.class);

    /**
     * cglib 跟jdk 的区别
     * 1 拦截器 接口不同 jdk是MethInvocation ，cglib是MethodInterceptor
     * 2 cglib 不需要去 实例话对象
     */
    Student  studentPoxy = (Student) enhancer.create();
    studentPoxy.doWork();

  }

  @org.junit.Test
  public void test1(){

    Student student = new Student("Tom");

    Enhancer enhancer = new Enhancer();
    enhancer.setCallback(new CustomInterceptor(student));
    enhancer.setSuperclass(Person.class);

    Person  studentPoxy = (Person) enhancer.create();
    studentPoxy.doWork();

  }

  @org.junit.Test
  public void test2(){

    Enhancer enhancer = new Enhancer();
    enhancer.setCallback(new CustomInterceptor());
    enhancer.setSuperclass(Person.class);

    /**
     * 把 Person接口 当成被代理类
     * 报错 java.lang.NoSuchMethodError: java.lang.Object.doWork()V
     */
    Person student = (Person) enhancer.create();
    student.doWork();

  }

}
